package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.BookDao;
import com.aaa.book_mall.dao.impl.BookDaoImpl;
import com.aaa.book_mall.entity.Book;
import com.aaa.book_mall.util.LayTab;
import com.aaa.book_mall.util.OSSUploadUtil;
import com.aaa.book_mall.util.Result;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: t_book表对应的controller
 * @ClassName: BookController
 * @Author: fengyun
 * @Date: 2021/1/19 19:03
 * @Version: 1.0
 */
@RestController
@RequestMapping("/book")
public class BookController {

    private BookDao bookDao = new BookDaoImpl();
    /* *
     *  查询t_book所有的所有信息
     * @param book
     * @return com.aaa.book_mall.util.LayTab
     * @date 2021/1/21 19:17
     */
    @RequestMapping("/showAllBook")
    public LayTab getAllbook(Book book){
        System.out.println(book);
        HashMap<Object, Object> map = new HashMap<>();
        List<Book> books = bookDao.queryAllBook(book);
        System.out.println(books);

//        查询表中的所有信息，用于分页
        int i = bookDao.allCount();
        LayTab layTab = new LayTab(i, books);
        return layTab;
    }

    /**
     * 将图书信息存在session中
     * @param
     * @param
     * @return
     */
    @RequestMapping("/showBook")
    public List<Book> showAllBook(HttpServletRequest request){
        System.out.println("进入session中开始存图书信息");
        //HashMap<Object, Object> map = new HashMap<>();
        List<Book> books = bookDao.showBook();
        //先将staff存储session中
        request.getSession().setAttribute("book",books);
        System.out.println(books);
        return books;
    }



    //请求的路径映射
    @RequestMapping("/getAllBook")
    protected LayTab getAllBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("getallbook");
        //获取请求值
        //获取所有请求的参数 （包含表单中所有的参数）
        Map<String, String[]> map = req.getParameterMap();
        Book book = new Book();
        try {
            BeanUtils.populate(book,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> allBook = bookDao.getAllBook(book);
        System.out.println(allBook);
        int count = bookDao.getBookCount(book);
        LayTab layTab = new LayTab(count,allBook);
        return layTab;

    }

    /* *
     *  根据bookid查询书本的数量
     * @param book
     * @return java.util.Map
     * @date 2021/1/21 19:20
     */
    @RequestMapping("/bookcount")
    public Map selectCountByid(@RequestBody Book book){
        int count = bookDao.selectCountByBookid(book.getBookid());
        HashMap<String, Object> sb = new HashMap<>();
        sb.put("count",count);
        return sb;
    }
    /**
     * 删除
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/deleteBook")
    public Map deleteBook(HttpServletRequest req, HttpServletResponse resp) {
        //获取id
        String id = req.getParameter("bookid");
        int i = bookDao.deleteBook(Integer.parseInt(id));
        HashMap<Object, Object> map = new HashMap<>();
        //判断删除是否成功
        if(i>0){
            map.put("code","1");
            map.put("msg","删除成功");
        }else{
            map.put("code","0");
            map.put("msg","删除失败");
        }
       /* Result result;
        //判断删除是否成功
        if(i>0){
            result = new Result("1","删除成功");
        }else {
            result = new Result("0", "删除失败");
        }*/

        return map;
    }

    /**
     * 修改
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/updateBook")
    public Map updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> maps = req.getParameterMap();
        Book book = new Book();

        try {
            BeanUtils.populate(book,maps);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int i = 0;
        if(book.getBookid()==0){
            i = bookDao.insertBook(book);
            System.out.println("修改状态"+i);
        }else {
            i = bookDao.updateBook(book);
        }
        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","修改成功");
        }else{
            map.put("code","0");
            map.put("msg","修改失败");
        }

        return map;
    }

    /**
     * 修改审核状态
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/updateStatus")
    protected Map updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数值
        String id = req.getParameter("bookid");
        String status = req.getParameter("status");
        int i = bookDao.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","修改成功");
        }else{
            map.put("code","0");
            map.put("msg","修改失败");
        }
      /*  Result result;
        if(i>0){
            result = new Result("1","修改成功");
        }else {
            result = new Result("0","修改失败");
        }*/

        return map;
    }

    /**
     * 图片上传，返回前台得到的url路径
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Map upload(MultipartFile file){
        System.out.println("进来了");
        //执行上传方法
        String url = OSSUploadUtil.upload2(file);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("imgurlCode",url);
        System.out.println(url);
        return map;
    }

   /* @RequestMapping("/showBook")
    public LayTab showBook(Book book, HttpServletRequest req){
        List<Map<String, Object>> maps = bookDao.showBook(book);
        req.setAttribute("maps",maps);
        int count = bookDao.getBookCount(book);
        LayTab layTab = new LayTab(count,maps);
        return layTab;
        //req.getRequestDispatcher("/showShop.jsp").forward(req,resp);
    }*/
}

